home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / FLTK-1.0.6 / src / Fl_Counter.cxx < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-07  |  4.7 KB  |  170 lines

  1. //
  2. // "$Id: Fl_Counter.cxx,v 1.8 1999/01/07 19:17:19 mike Exp $"
  3. //
  4. // Counter widget for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-1999 by Bill Spitzak and others.
  7. //
  8. // This library is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Library General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 2 of the License, or (at your option) any later version.
  12. //
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. // Library General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Library General Public
  19. // License along with this library; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. // USA.
  22. //
  23. // Please report all bugs and problems to "fltk-bugs@easysw.com".
  24. //
  25.  
  26. #include <FL/Fl.H>
  27. #include <FL/Fl_Counter.H>
  28. #include <FL/fl_draw.H>
  29.  
  30. void Fl_Counter::draw() {
  31.   int i; Fl_Boxtype boxtype[5];
  32.   Fl_Color selcolor;
  33.  
  34.   boxtype[0] = box();
  35.   if (boxtype[0] == FL_UP_BOX) boxtype[0] = FL_DOWN_BOX;
  36.   for (i=1; i<5; i++)
  37.     if (mouseobj == i)
  38.       boxtype[i] = down(box());
  39.     else
  40.       boxtype[i] = box();
  41.  
  42.   int xx[5], ww[5];
  43.   if (type() == FL_NORMAL_COUNTER) {
  44.     int W = w()*15/100;
  45.     xx[1] = x();     ww[1] = W;
  46.     xx[2] = x()+1*W;     ww[2] = W;
  47.     xx[0] = x()+2*W;     ww[0] = w()-4*W;
  48.     xx[3] = x()+w()-2*W; ww[3] = W;
  49.     xx[4] = x()+w()-1*W; ww[4] = W;
  50.   } else {
  51.     int W = w()*20/100;
  52.     xx[2] = x();     ww[2] = W;
  53.     xx[0] = x()+W;     ww[0] = w()-2*W;
  54.     xx[3] = x()+w()-1*W; ww[3] = W;
  55.   }
  56.  
  57.   draw_box(boxtype[0], xx[0], y(), ww[0], h(), FL_WHITE);
  58.   fl_font(textfont(), textsize());
  59.   fl_color(active_r() ? textcolor() : inactive(textcolor()));
  60.   char str[128]; format(str);
  61.   fl_draw(str, xx[0], y(), ww[0], h(), FL_ALIGN_CENTER);
  62.   if (!(damage()&FL_DAMAGE_ALL)) return; // only need to redraw text
  63.  
  64.   if (active_r())
  65.     selcolor = labelcolor();
  66.   else
  67.     selcolor = inactive(labelcolor());
  68.  
  69.   if (type() == FL_NORMAL_COUNTER) {
  70.     draw_box(boxtype[1], xx[1], y(), ww[1], h(), color());
  71.     fl_draw_symbol("@-4<<", xx[1], y(), ww[1], h(), selcolor);
  72.   }
  73.   draw_box(boxtype[2], xx[2], y(), ww[2], h(), color());
  74.   fl_draw_symbol("@-4<",  xx[2], y(), ww[2], h(), selcolor);
  75.   draw_box(boxtype[3], xx[3], y(), ww[3], h(), color());
  76.   fl_draw_symbol("@-4>",  xx[3], y(), ww[3], h(), selcolor);
  77.   if (type() == FL_NORMAL_COUNTER) {
  78.     draw_box(boxtype[4], xx[4], y(), ww[4], h(), color());
  79.     fl_draw_symbol("@-4>>", xx[4], y(), ww[4], h(), selcolor);
  80.   }
  81. }
  82.  
  83. void Fl_Counter::increment_cb() {
  84.   if (!mouseobj) return;
  85.   double v = value();
  86.   switch (mouseobj) {
  87.   case 1: v -= lstep_; break;
  88.   case 2: v = increment(v, -1); break;
  89.   case 3: v = increment(v, 1); break;
  90.   case 4: v += lstep_; break;
  91.   }
  92.   handle_drag(clamp(round(v)));
  93. }
  94.  
  95. #define INITIALREPEAT .5
  96. #define REPEAT .1
  97.  
  98. void Fl_Counter::repeat_callback(void* v) {
  99.   Fl_Counter* b = (Fl_Counter*)v;
  100.   if (b->mouseobj) {
  101.     Fl::add_timeout(REPEAT, repeat_callback, b);
  102.     b->increment_cb();
  103.   }
  104. }
  105.  
  106. int Fl_Counter::calc_mouseobj() {
  107.   if (type() == FL_NORMAL_COUNTER) {
  108.     int W = w()*15/100;
  109.     if (Fl::event_inside(x(), y(), W, h())) return 1;
  110.     if (Fl::event_inside(x()+W, y(), W, h())) return 2;
  111.     if (Fl::event_inside(x()+w()-2*W, y(), W, h())) return 3;
  112.     if (Fl::event_inside(x()+w()-W, y(), W, h())) return 4;
  113.   } else {
  114.     int W = w()*20/100;
  115.     if (Fl::event_inside(x(), y(), W, h())) return 2;
  116.     if (Fl::event_inside(x()+w()-W, y(), W, h())) return 3;
  117.   }
  118.   return -1;
  119. }
  120.  
  121. int Fl_Counter::handle(int event) {
  122.   int i;
  123.   switch (event) {
  124.   case FL_RELEASE:
  125.     if (mouseobj) {
  126.       Fl::remove_timeout(repeat_callback, this);
  127.       mouseobj = 0;
  128.       redraw();
  129.     }
  130.     handle_release();
  131.     return 1;
  132.   case FL_PUSH:
  133.     handle_push();
  134.   case FL_DRAG:
  135.     i = calc_mouseobj();
  136.     if (i != mouseobj) {
  137.       Fl::remove_timeout(repeat_callback, this);
  138.       mouseobj = i;
  139.       if (i) Fl::add_timeout(INITIALREPEAT, repeat_callback, this);
  140.       increment_cb();
  141.       redraw();
  142.     }
  143.     return 1;
  144.   default:
  145.     return 0;
  146.   }
  147. }
  148.  
  149. Fl_Counter::~Fl_Counter() {
  150.   Fl::remove_timeout(repeat_callback, this);
  151. }
  152.  
  153. Fl_Counter::Fl_Counter(int x, int y, int w, int h, const char* l)
  154.   : Fl_Valuator(x, y, w, h, l) {
  155.   box(FL_UP_BOX);
  156.   selection_color(FL_INACTIVE_COLOR); // was FL_BLUE
  157.   align(FL_ALIGN_BOTTOM);
  158.   bounds(-1000000.0, 1000000.0);
  159.   Fl_Valuator::step(1, 10);
  160.   lstep_ = 1.0;
  161.   mouseobj = 0;
  162.   textfont_ = FL_HELVETICA;
  163.   textsize_ = FL_NORMAL_SIZE;
  164.   textcolor_ = FL_BLACK;
  165. }
  166.  
  167. //
  168. // End of "$Id: Fl_Counter.cxx,v 1.8 1999/01/07 19:17:19 mike Exp $".
  169. //
  170.